package at.sii.n4w;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class WriteFragment extends Fragment {

	private static Tag tag;
	public static final String TAG = "NFC4WLAN";
	View rootView;

	private String f_stationID = "4643"; // U6 Direction FLORIDSDORF
	private String s_stationID = "4648"; // U6 Direction SIEBENHIRTEN

	public WriteFragment() {

	}

	public static Fragment newInstance(Tag _tag) {
		tag = _tag;
		WriteFragment fragment = new WriteFragment();
		return fragment;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_write, container, false);

		final EditText ssid_edit = (EditText) rootView
				.findViewById(R.id.ssid_edit);
		final EditText pwd_edit = (EditText) rootView
				.findViewById(R.id.pwd_edit);
		final EditText station1_edit = (EditText) rootView
				.findViewById(R.id.station1_edit);
		final EditText station2_edit = (EditText) rootView
				.findViewById(R.id.station2_edit);
		Button btnWrite = (Button) rootView.findViewById(R.id.write_button);
		btnWrite.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					if (tag != null) {
						String tmp = ssid_edit.getText().toString() + "@#@"
								+ pwd_edit.getText().toString();
						if (station1_edit.getText().toString() != "") {
							tmp += "@#@" + station1_edit.getText().toString();
							if (station2_edit.getText().toString() != "") {
								tmp += "@#@"
										+ station2_edit.getText().toString();
							} else {
								tmp += "@#@" + f_stationID;
							}
						} else if (station2_edit.getText().toString() != "") {
							tmp += "@#@" + station2_edit.getText().toString() + "@#@" + s_stationID;
						} else {
							tmp += "@#@" + f_stationID + "@#@" + s_stationID;
						}
						Log.d(TAG, "written tag: " + tmp);
						write(tmp, tag);
					}
				} catch (IOException e) {
					e.printStackTrace();
				} catch (FormatException e) {
					e.printStackTrace();
				}
			}
		});

		return rootView;
	}

	public void onResume() {
		super.onResume();
		final EditText ssid_edit = (EditText) rootView
				.findViewById(R.id.ssid_edit);
		ssid_edit.setText("");
		final EditText pwd_edit = (EditText) rootView
				.findViewById(R.id.pwd_edit);
		pwd_edit.setText("");
		final EditText station1_edit = (EditText) rootView
				.findViewById(R.id.station1_edit);
		station1_edit.setText("");
		final EditText station2_edit = (EditText) rootView
				.findViewById(R.id.station2_edit);
		station2_edit.setText("");
		Log.d(TAG, "write onResume");
	}

	public void onPause() {
		super.onPause();
		Log.d(TAG, "write onPause");
	}

	private NdefRecord createRecord(String text)
			throws UnsupportedEncodingException {

		// create the message in according with the standard
		String lang = "en";
		byte[] textBytes = text.getBytes();
		byte[] langBytes = lang.getBytes("US-ASCII");
		int langLength = langBytes.length;
		int textLength = textBytes.length;

		byte[] payload = new byte[1 + langLength + textLength];
		payload[0] = (byte) langLength;

		// copy langbytes and textbytes into payload
		System.arraycopy(langBytes, 0, payload, 1, langLength);
		System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);

		NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
				NdefRecord.RTD_TEXT, new byte[0], payload);
		return recordNFC;
	}

	private void write(String text, Tag tag) throws IOException,
			FormatException {

		NdefRecord[] records = { createRecord(text) };
		NdefMessage message = new NdefMessage(records);
		Ndef ndef = Ndef.get(tag);
		ndef.connect();
		ndef.writeNdefMessage(message);
		ndef.close();
		Toast.makeText(getActivity(), "Tag was written!", Toast.LENGTH_SHORT)
				.show();
	}

	public void refreshData(Tag tag2) {
		tag = tag2;
	}
}
